package com.zhan.ielts.web.controller.word;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.ielts.domain.query.word.DictionaryQuery;
import com.zhan.ielts.domain.word.Dictionary;
import com.zhan.ielts.service.word.DictionaryService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.http.util.TextUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController extends BaseControllerImpl<Dictionary, DictionaryQuery> {

	private Logger log = LoggerFactory.getLogger(DictionaryController.class);

	@Autowired
	private DictionaryService dictionaryService;

	@Override
	protected BaseService<Dictionary> getBaseService() {
		return dictionaryService;
	}


	@Override
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView selectList(DictionaryQuery query, @PageableDefault Pageable pageable) {
		ModelAndView mav = null;
		try {
			Page<Dictionary> page = getBaseService().queryPageList(query, pageable);
			mav = new ModelAndView("/word/dictionary/listDictionary", "page", page);
			mav.addObject("query", query);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}

	@Override
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView editView(@PathVariable("id") String id) {
		Object obj = getBaseService().queryById(id);
		return new ModelAndView("/word/dictionary/editDictionary", path.getEntityName(), obj);
	}


	@Override
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView addOne(Dictionary entity) {
		try {
			getBaseService().insert(entity);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ModelAndView(path.getRedirectListPath());
	}


	@RequestMapping(value = "/updateOne", method = RequestMethod.POST)
	public ModelAndView editWord(DictionaryQuery query) {
		String examplesTemp = query.getExamples();
		String tempArr[]  = examplesTemp.split("(e|c)n:");
		JSONArray array = new JSONArray();
		for (int i = 0; i < tempArr.length; i++) {
			if(i > 0) {
				if (i % 2 == 1) {
					JSONObject object = new JSONObject();
					object.put("en", tempArr[i].replace("\r\n", ""));
					array.add(object);
				} else {
					((JSONObject)array.get(array.size() - 1)).put("cn", tempArr[i].replace("\r\n", ""));
				}
			}
		}
		query.setExamples(array.toString());
		if (TextUtils.isEmpty(query.getId())){
			getBaseService().insert(query);
		} else {
			getBaseService().updateByIdSelective(query);
		}
		return new ModelAndView("redirect:/dictionary");
	}


	@ResponseBody
	@RequestMapping(value = "/extract", method = RequestMethod.POST)
	public String extract() {
		log.debug("字典开始！");

		Dictionary dictionaryTemp = new Dictionary();
		dictionaryService.extract(dictionaryTemp);

		log.debug("字典结束！");
		return "success!";
	}

	
	@ResponseBody
	@RequestMapping(value = "/import", method = RequestMethod.POST)
	public String importFromExcel() {
		log.debug("导入字典开始！");

		dictionaryService.importFromExcel();

		log.debug("导入字典结束！");
		return "success!";
	}
	
	
	@ResponseBody
	@RequestMapping(value = "/updateField", method = RequestMethod.POST)
	public String updateField() {
		log.debug("字典开始！");

		Dictionary dictionaryTemp = new Dictionary();
		dictionaryService.updateField(dictionaryTemp);

		log.debug("字典结束！");
		return "success!";
	}
	
	
}
